<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="10">
        <el-col :span="8"
          ><el-input v-model="requestInfo.query" placeholder="请输入内容"><el-button slot="append" icon="el-icon-search" @click="searchGoodFn"></el-button></el-input
        ></el-col>
        <el-col :span="4"><el-button type="primary" @click="gotoAdd">添加商品</el-button></el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table border stripe :data="goodsList" style="width: 100%">
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"> </el-table-column>
        <el-table-column prop="add_time" width="185" label="创建时间"> 
          <template slot-scope="scope">
            <!-- 使用全局过滤器自定义时间 -->
            {{scope.row.add_time | dateFormate}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button @click="deleteItemFn(scope.row.goods_id)" size="mini" type="danger" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="requestInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="requestInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getGoodsList , deleteGoodByid} from '@/api/index.js'
export default {
  data() {
    return {
      //*商品列表请求参数
      requestInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      //*商品列表
      goodsList: [],
      //*总数据条数
      total: 0,
    }
  },
  created() {
    this.getGoodsListFn()
  },
  methods: {
    //*跳转路由
    gotoAdd(){
      this.$router.push('/home/goods/add')
    },
    //*查询商品
    searchGoodFn(){
      this.getGoodsListFn()
    },
    //*删除商品
    async deleteItemFn(id){
    let {data:res} =  await deleteGoodByid(id)
      if(res.meta.status != 200) {return this.$message.error('删除数据失败')}
      this.$message.success("删除数据成功")
      this.getGoodsListFn()
    },
    //*当前页改变
    handleCurrentChange(newCurrentPage) {
      this.requestInfo.pagenum = newCurrentPage
      this.getGoodsListFn()
    },
    //*当前页数据条数改变
    handleSizeChange(newPagesize) {
      this.requestInfo.pagesize = newPagesize
      this.getGoodsListFn()
    },
    //*获取商品数据列表
    async getGoodsListFn() {
      let { data: res } = await getGoodsList(this.requestInfo)
      if (res.meta.status != 200) {
        return this.$message.console.error('获取数据失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
  },
}
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 15px;
}
.el-table {
  margin-top: 12px;
}
.el-pagination {
  text-align: center;
  margin-top: 15px;
}
</style>
